<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium"
      class="ry_form">
      <el-form-item label="分类编码" prop="number">
        <el-input v-model.trim="queryParams.number" placeholder="请输入分类编码" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model.trim="queryParams.name" placeholder="请输入分类名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="父节点" prop="parentId">
        <el-input v-model.trim="parentName" disabled size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="显示排序" prop="orderNo">
        <el-input v-model.trim="queryParams.orderNo" placeholder="请输入显示排序" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button size="mini" @click="handleQuery">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleAdd" v-hasPermi="['wms:AssetCategory:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:AssetCategory:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:AssetCategory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['wms:AssetCategory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="Refresh" :columns="columns"></right-toolbar>
    </el-row>

    <vxe-table v-loading="loading" :data="AssetCategoryList" @selection-change="handleSelectionChange">
      <vxe-column type="checkbox" width="55" align="center"></vxe-column>
      <vxe-column field="number" title="分类编码" align="center" v-if="columns[0].visible"></vxe-column>
      <vxe-column field="name" title="分类名称" align="center" v-if="columns[1].visible"></vxe-column>
      <vxe-column field="parentId" title="父节点ID" align="center" v-if="columns[2].visible"></vxe-column>
      <vxe-column field="orderNo" title="显示排序" align="center" v-if="columns[3].visible"></vxe-column>
      <vxe-column field="remark" title="备注" align="center" v-if="columns[4].visible"></vxe-column>
      <vxe-column title="操作" align="center" width="150">
        <template #default="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)"
            v-hasPermi="['wms:AssetCategory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)"
            v-hasPermi="['wms:AssetCategory:remove']">删除</el-button>

        </template>
      </vxe-column>
    </vxe-table>
    <div class="pagination-container">
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改资产分类表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model.trim="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNo">
          <el-input v-model.trim="form.orderNo" placeholder="请输入显示排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listAssetCategory, getAssetCategory, delAssetCategory, addAssetCategory, updateAssetCategory, exportAssetCategory } from "@/api/asset/assetCategory.js";

export default {
  name: "AssetCategory",
  props: {
    parentNode: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      AssetCategoryList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        name: null,
        parentId: null,
        orderNo: null,
      },
      form: {},
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
      },
      columns: [
        { key: 1, label: "分类编码", visible: true },
        { key: 2, label: "分类名称", visible: true },
        { key: 3, label: "父节点ID", visible: true },
        { key: 4, label: "显示排序", visible: true },
        { key: 5, label: "备注", visible: true },
      ],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      const { pageNum, pageSize } = this.queryParams;
      if (this.parentNode) {
        this.queryParams.parentId = this.parentNode.id;
        this.queryParams.parentNumber = this.parentNode.number;
      }
      const query = { ...this.queryParams, pageNum, pageSize };
      const pageReq = { page: pageNum - 1, size: pageSize };
      listAssetCategory(query, pageReq).then(response => {
        const { content, totalElements } = response;
        this.AssetCategoryList = content;
        this.total = totalElements;
        this.loading = false;

      }).catch(error => {
        this.loading = false;
        console.log(error);
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        number: null,
        name: null,
        parentId: null,
        orderNo: null,
        remark: null,
      };
      this.resetForm("form");
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleAdd() {
      this.title = "新增分类";
      this.open = true;
      this.reset();
    },
    handleUpdate(row) {
      this.title = "修改分类";
      this.open = true;
      this.getForm(row.id);
    },
    Refresh() {
      this.$message.success("刷新成功");
    },
  
    handleDelete(row) {
      const ids = row ? [row.id] : this.ids;
      if (ids.length > 0) {
        this.$confirm("是否删除选中分类?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        }).then(() => {
          delAssetCategory(ids).then(() => {
            this.$message.success("删除成功");
            this.getList();
          }).catch(() => {
            this.$message.error("删除失败");
          });
        });
      } else {
        this.$message.warning("请选择要删除的分类");
      }
    },
    handleSelectionChange(rows) {
      this.ids = rows.map(row => row.id);
      this.single = this.ids.length === 1;
      this.multiple = this.ids.length > 0;
    },
    getForm(id) {
      getAssetCategory(id).then(response => {
        this.form = response;
      }).catch(error => {
        console.log(error);
      });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const submitMethod = this.form.id ? updateAssetCategory : addAssetCategory;
          submitMethod(this.form).then(() => {
            this.$message.success(`${this.title}成功`);
            this.open = false;
            this.getList();
          }).catch(() => {
            this.$message.error(`${this.title}失败`);
          });
        }
      });
    },
    handleExport() {
      this.exportLoading = true;
      exportAssetCategory().then(() => {
        this.$message.success("导出成功");
      }).catch(() => {
        this.$message.error("导出失败");
      }).finally(() => {
        this.exportLoading = false;
      });
    },
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh;
  overflow: hidden;
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
}
</style>